﻿package org.cubeland.game.core.drawing.yuki 
{
	import flash.display.Bitmap;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Cube;

	import caurina.transitions.Tweener;
	/**
	 * CubeLand.org Game
	 * @author Duarte Peixinho
	 */
	public class DrawYukiFeet extends DisplayObject3D
	{
		
		[Embed(source = '../../../assets/yuki/footTop.jpg')]
		private var YukiFootTop:Class;
		[Embed(source = '../../../assets/yuki/footBottom.jpg')]
		private var YukiFootBottom:Class;
		[Embed(source = '../../../assets/yuki/footSide.jpg')]
		private var YukiFootSide:Class;
		
		private var _leftFoot:Cube;
		private var _rightFoot:Cube;
		private var _angleLeft:Object;
		private var _angleRight:Object;
		private var _movingLeft:Boolean=false;
		private var _movingRight:Boolean=false;
		
		private const TIME_MOVE:Number = 0.5;
		
		public function DrawYukiFeet() 
		{
			_angleLeft={angle: 0};
			_angleRight={angle: 0};
			
			init();
		}

		public function init():void {
			
			_leftFoot = new Cube(drawMaterials(),50,50,20);
			_rightFoot = new Cube(drawMaterials(), 50,50,20);
			_leftFoot.x -= 30;
			_rightFoot.x += 30;
			addChild(_leftFoot);
			addChild(_rightFoot);
		}
		
		private function drawMaterials():MaterialsList {		
			var BBottom:Bitmap = new YukiFootBottom();
			var BTop:Bitmap = new YukiFootTop();
			var BSide:Bitmap = new YukiFootSide();
			
			var BBottomD:BitmapMaterial = new BitmapMaterial(BBottom.bitmapData);	
			var BTopD:BitmapMaterial = new BitmapMaterial(BTop.bitmapData);
			var BSideD:BitmapMaterial = new BitmapMaterial(BSide.bitmapData);
			
			BBottomD.smooth = true;
			BTopD.smooth = true;
			BSideD.smooth = true;
			
			var m:MaterialsList = new MaterialsList( {back: BSideD,front:BSideD,top:BTopD,bottom:BBottomD,left:BSideD,right:BSideD} );			
			return m;		
		}
		
		public function walk():void {
			walkRightUp();
		}
		
		public function stop():void {
			// stop left

			// stop right

			_leftFoot.y=0;
			_rightFoot.y= 0;
			_leftFoot.z= 0;
			_rightFoot.z=0;
		}

		private function walkRightUp():void {
			_movingRight=true;
			walkLeftBack();
			Tweener.addTween(_angleRight,{angle:Math.PI,time:TIME_MOVE,onComplete:walkRightBack,transition:"linear"  });	
		}
		private function walkLeftUp():void {
			_movingLeft=true;
			Tweener.addTween(_angleLeft, {angle:Math.PI,time:TIME_MOVE,transition:"linear"  });
		}
		private function walkRightBack():void {
			_movingRight=false;
			_angleLeft.angle=0;
			walkLeftUp();
			Tweener.addTween(_rightFoot,{time:TIME_MOVE,z:Math.cos(0) *60,y:Math.sin(0) *30,onComplete:walkRightUp,transition:"linear"  });
		}
		private function walkLeftBack():void {
			_movingLeft=false;
			_angleRight.angle=0;
			Tweener.addTween(_leftFoot, {time:TIME_MOVE,z:Math.cos(0) *60,y:Math.sin(0) *30,transition:"linear" });
		}
		public function updateFeetPos():void {
			
			if (_movingLeft) {
				_leftFoot.z = Math.cos(_angleLeft.angle) *60;
				_leftFoot.y = Math.sin(_angleLeft.angle) *30;
			}
			if (_movingRight) {
				_rightFoot.z = Math.cos(_angleRight.angle) *60;
				_rightFoot.y = Math.sin(_angleRight.angle) *30; 
			}

		}
		
	}

}